package study.linux.congty.kadai.source.postgresql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQLvsPostgreSQL {
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String schema = "testdb";
		String table = "testtable";
		String[] fields = { "id", "title" };
		int length = fields.length;

		long start;
		long end;
		long time;

		System.out.println("========== MYSQL ============");
		start = System.currentTimeMillis();
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String connectionUrl = "jdbc:mysql://localhost:3306/" + schema;
			String connectionUser = "test";
			String connectionPassword = "pw";
			conn = DriverManager.getConnection(connectionUrl, connectionUser,
					connectionPassword);
			stmt = conn.createStatement();
			rs = stmt.executeQuery("SELECT * FROM " + table);
			while (rs.next()) {
				for (int i = 0; i < length; i++) {
					String field = fields[i];
					String val = rs.getString(field);
					System.out.print("<p>" + field + ": " + val + "<p>");
				}
				System.out.println();
			}
		} catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
			return;
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		end = System.currentTimeMillis();
		time = end - start;

		System.out.println("======== PostgreSQL =========");

		start = System.currentTimeMillis();
		try {
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection(
					"jdbc:postgresql://localhost/testdb", "test",
					"test");
			stmt = conn.createStatement();
			String sqlStr = "SELECT * FROM testtable";
			rs = stmt.executeQuery(sqlStr);
			while (rs.next()) {
				for (int i = 0; i < length; i++) {
					String field = fields[i];
					String val = rs.getString(field);
					System.out.print("<p>" + field + ": " + val + "<p>");
				}
				System.out.println();
			}

		} catch (Exception e) {
			System.out.println(e.toString());
			return ;
			// TODO: handle exception
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		end = System.currentTimeMillis();
		System.out.println("+===========================+");
		System.out.println("==== !! Please wait !!  =====");
		  System.out.print("          compare");
		try {
			for (int i = 0; i < 5; i++) {
				Thread.sleep(1000);
				System.out.print(".");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ;
		}
		System.out.println("\nMYSQL : " + time + " millis");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("PostgreSQL : " + (end - start) + " millis");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long comp = time - (end - start);
		String compStr = (comp == 0) ? "=" : ( (comp > 0) ? "<" : ">");
		System.out.println("Speed comparison result result: MYSQL " + compStr + " PostgreSQL");
		System.out.println("siranakatta");
	}

}
